package com.msb.dongbao.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.dongbao.common.base.enums.StateCodeEnum;
import com.msb.dongbao.common.base.result.ResultWrapper;
import com.msb.dongbao.common.util.JwtUtil;
import com.msb.dongbao.ums.entity.UmsMember;
import com.msb.dongbao.ums.entity.dto.UmsMemberLoginParamDTO;
import com.msb.dongbao.ums.entity.dto.UmsMemberRegisterParamDTO;
import com.msb.dongbao.ums.entity.response.UserMemberLoginResponse;
import com.msb.dongbao.ums.mapper.UmsMemberMapper;
import com.msb.dongbao.ums.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author 马雨
 * @since 2021-05-25
 */
@Service
@Transactional
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember> implements UmsMemberService {

    @Autowired
    UmsMemberMapper umsMemberMapper;
    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public ResultWrapper register(UmsMemberRegisterParamDTO umsMemberParamDTO) {
        LambdaQueryWrapper<UmsMember> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(UmsMember::getUsername, umsMemberParamDTO.getUsername());
        UmsMember umsMember = umsMemberMapper.selectOne(lambdaQuery);

        if (Objects.nonNull(umsMember)) {
            return ResultWrapper.getFailBuilder().code(StateCodeEnum.DUPLICATE_USERNAME.getCode())
                    .msg(StateCodeEnum.DUPLICATE_USERNAME.getMsg()).build();
        }

        umsMember = new UmsMember();
        BeanUtils.copyProperties(umsMemberParamDTO, umsMember);

        //密码加密
        //BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(umsMember.getPassword());
        umsMember.setPassword(encode);

        umsMemberMapper.insert(umsMember);
        return ResultWrapper.getSuccessBuilder().code(StateCodeEnum.SUCCESS.getCode())
                .msg(StateCodeEnum.SUCCESS.getMsg()).build();
    }

    @Override
    public ResultWrapper login(UmsMemberLoginParamDTO umsMemberLoginParamDTO) {
        LambdaQueryWrapper<UmsMember> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(UmsMember::getUsername, umsMemberLoginParamDTO.getUsername());
        UmsMember umsMember = umsMemberMapper.selectOne(lambdaQuery);

        if (Objects.nonNull(umsMember)) {
            String password = umsMember.getPassword();
            if (!passwordEncoder.matches(umsMemberLoginParamDTO.getPassword(), password)) {
                return ResultWrapper.getFailBuilder().code(StateCodeEnum.PASSWORD_ERROR.getCode())
                        .msg(StateCodeEnum.PASSWORD_ERROR.getMsg()).build();
            }
        } else {
            return ResultWrapper.getFailBuilder().code(StateCodeEnum.USER_EMPTY.getCode())
                    .msg(StateCodeEnum.USER_EMPTY.getMsg()).build();
        }

        String token = JwtUtil.createToken(umsMember.getId() + "");
        UserMemberLoginResponse umlr = new UserMemberLoginResponse();
        umlr.setToken(token);
        umsMember.setPassword("");
        umlr.setUmsMember(umsMember);
        return ResultWrapper.getSuccessBuilder().data(umlr).build();
    }

    @Override
    public ResultWrapper edit(UmsMember umsMember) {
        umsMemberMapper.updateById(umsMember);
        return ResultWrapper.getSuccessBuilder().data(umsMember).build();
    }
}
